const util = require('../../../../../utils/util.js');
const api = require('../../../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    labelListValue: [],
    labelList: [],
    catalogId: "",
    catalogName: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    // 获取所有类目标签
    _this.allCatalogLabel();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 标签选择改变背景图片
   */
  labelListChange(event) {
    var _this = this;
    var variableData = parseInt(event.currentTarget.dataset.variable);
    var imageChick = _this.data.labelList[variableData].imageChick;
    var imageNoClick = _this.data.labelList[variableData].imageNoClick;
    var labelListValue = _this.data.labelListValue;
    var flag = false;
    for (var i = 0; i < labelListValue.length; i++) {
      if (labelListValue[i] == variableData) {
        flag = true;
      }
    }
    if (flag) {
      _this.setData({
        ['labelList[' + variableData + '].image']: imageChick,
        ['labelList[' + variableData + '].borderBottomClass']: "border_bottom_none"
      });
    } else {
      _this.setData({
        ['labelList[' + variableData + '].image']: imageNoClick,
        ['labelList[' + variableData + '].borderBottomClass']: ""
      });
    }
  },

  /**
   * 父标签选择改变标签值
   */
  labelCollapseChange: function (event) {
    var _this = this;
    _this.setData({
      labelListValue: event.detail
    });
  },

  /**
   * 子标签选择
   */
  labelTagChange(event) {
    var _this = this;
    var pindex = event.currentTarget.dataset.pindex;
    var index = event.currentTarget.dataset.index;
    var catalogId = event.currentTarget.dataset.id;
    var catalogName = _this.data.labelList[pindex].tags[index].name;
    _this.setData({
      catalogId: catalogId,
      catalogName: catalogName,
      ["labelList[" + pindex + "].showCheakedText"]: catalogName
    })
  },

  /**
   * 确定按钮点击事件
   */
  success: function(){
    var _this = this;
    var labelListValue = this.data.labelListValue;
    if (labelListValue.length > 0){
      var pages = getCurrentPages();
      var catalogId = _this.data.catalogId;
      var catalogName = _this.data.catalogName;
      var prevPage = pages[pages.length - 2];  //上一个页面
      prevPage.setData({
        catalogId: catalogId,
        catalogName: catalogName
      });
      wx.navigateBack({
        delta: 1
      })
    }
  },

  /**
   * 获取所有类目标签
   */
  allCatalogLabel: function(){
    var _this = this;
    util.post(api.Home_AllCatalogLabelUrl).then(function (res) {
      if (res.code == 200) {
        var list = res.result.personalityLabelList;
        _this.setData({
          labelList: list
        })
      }
    })
  }
})